﻿using System.Collections.Generic;
using System.Linq;
using System.Data;
using org.se.fs.presenter.IView.Template;
using System;
using org.se.fs.presenter.Library;
using org.se.fs.presenter.Task.Template;

namespace org.se.fs.presenter.View.Profile
{
    public class InstitutionLists : Template.ViewLists
    {
        public InstitutionLists(IFormLists view) : this(view, new Task.Context.Institution()) { }

        public InstitutionLists(IFormLists view, Crud task)
        {
            View = view;
            Task = task;
        }

        protected override void InitializeSearchOption()
        {
            SearchOption = new List<SearchOption>
                                {
                                    new SearchOption("Node", "name", typeof (string)),
                                    new SearchOption("Code", "code", typeof (string)),
                                    new SearchOption("Address", "address", typeof (string)),
                                    new SearchOption("Branch Name", "branch_id", typeof (UInt32)),
                                    new SearchOption("Who Create", "user_create_id", typeof (UInt32))
                                };
            var type = (SearchOption.Select(a => a.Option)).ToArray();
            View.FillType(type);
            View.IndexType = -1;
        }

        public override void DisplayData()
        {
            View.StatusLists = string.Format("Result(s): {0} found", Lists.Rows.Count);

            if (Lists.Rows.Count.Equals(0))
                View.Lists = new DataTable();

            SetStatusPage();

            var a = (Lists.AsEnumerable().OrderBy(b => b.Field<string>("code")).OrderBy(b => b.Field<string>("name")));
            Lists = a.CopyToDataTable();
            var d = (Lists.AsEnumerable().Select(c => new
            {
                Name = c.Field<string>("name"),
                Code = c.Field<string>("code"),
                Branch = String.Format("{0}-{1}", c.Field<string>("branch_name"),
                          c.Field<string>("branch_code")),
                Address = c.Field<string>("address"),
                UserName = c.Field<string>("user_name")
            })).ToArray();
            View.Lists = d;
        }
    }
}
